home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////
- //
- // SAPlayMovie.c written by Donald O. Olson
- // A simple QuickTime Scripting Addition written
- // to illustrate writing Scripting Additions.
- //
- // Copyright ®1993 Donald O. Olson
- // All rights reserved.
- //
- //////////////////////////////////////////////////////////////////
-
- // Our includes
- #include <Movies.h>
- #include <Memory.h>
- #include <Fonts.h>
- #include <OSEvents.h>
- #include <limits.h>
- #include <Menus.h>
- #include <Processes.h>
- #include <String.h>
- #include <Resources.h>
- #include <Packages.h>
- #include <AppleEvents.h>
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <Files.h>
-
- // Our optional parameters keyword
- #define keyLocation 'LOCA'
-
- // Our rectangle records keywords
- #define keyRight 'RGHT'
- #define keyLeft 'LEFT'
- #define keyTop 'TOP '
- #define keyBottom 'BOTM'
-
- #define kLeft 1 // Our list of points are in the
- #define kTop 2 // order shown
- #define kRight 3
- #define kBottom 4
- #define kDefaultOffset 100 // Used to position movie on screen
- #define kBogusNumber UINT_MAX
-
- /*
- Our prototypes, could be in a .h file but are included here
- for ease of use.
- */
-
- OSErr PlayTheMovie( FSSpec myFSSpec, AEDesc theLocationDesc);
- OSErr SetMovieRect( AEDesc theLocationDesc, Movie theMovie,
- Rect *ourRect);
-
- ////////////////////////////////////////////////////////////////////
- //
- // main()
- // The entry to our Scripting Addition.
- // Remember to declare it pascal!!
- //
- //////////////////////////////////////////////////////////////////
-
- pascal OSErr main( AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSErr theErr = noErr;
- FSSpec theFSSpec;
- DescType typeCode;
- long theGestaltReturn = 0;
- Size actualSize;
- AEDesc theLocationDesc;
-
- /* Is QuickTime present? */
- theErr = Gestalt(gestaltQuickTime, &theGestaltReturn);
- if(theErr) return theErr; // If not, bail.
-
- /*
- Grab the movie file's path from the direct parameter.
- We declared the direct parameter to be an alias in our
- aete. Since the AEM will coerce an alias to a FSSpec for
- us, and that's what the OpenMovieFile call wants, we'll
- ask for it as an FSSpec.
- */
-
- theErr = AEGetParamPtr( theEvent, keyDirectObject,
- typeFSS, &typeCode, (Ptr)&theFSSpec,
- sizeof(FSSpec), &actualSize);
- if(theErr) return theErr;
-
- /*
- Now get the location parameter, if it's present.
- We don't check errors for this call since the AEM
- will return the descriptor with the descriptorType
- field set to typeNull if there is an error. We check
- for NULL in the PlayTheMovie function.
- */
-
- theErr = AEGetParamDesc( theEvent, keyLocation,
- typeWildCard, &theLocationDesc);
-
- /* Start up the movie tools */
- if(EnterMovies()) return theErr; // Bail on error.
-
- theErr = PlayTheMovie(theFSSpec, theLocationDesc);
-
- ExitMovies(); // Close our connection to the movie tools
-
- return theErr; // And return our error.
- }
-
- ////////////////////////////////////////////////////////////////////
- //
- // PlayTheMovie() Opens and plays Movie File
- // This code is based on the SimplePlayer sample
- // that comes with the QuickTime Developers Disk.
- //
- //////////////////////////////////////////////////////////////////
-
- OSErr PlayTheMovie(FSSpec myFSSpec, AEDesc theLocationDesc)
- {
- Movie theMovie;
- Rect dispBounds;
- WindowPtr movieWindow = NULL;
- OSErr theErr = noErr;
- short resRefNum;
- long duration = 60, finalTick;
-
- /* Open the movie file */
- if(OpenMovieFile(&(myFSSpec), &resRefNum, 0)) {
- SysBeep(1); // Signal our error
- return theErr; // And bail
- }
-
- if(NewMovieFromFile( &theMovie, resRefNum,
- NULL, NULL,0, NULL )) {
- SysBeep(1); // Signal our error
- return theErr; // And bail
- }
-
- /* Get the bounds for the movie. */
-
- GetMovieBox( theMovie, &dispBounds);
-
- /*
- If the user passed in a location or size for the window,
- let's grab it now.
- */
-
- if(theLocationDesc.dataHandle != NULL) {
- // Use the values sent to us by the user.
- theErr = SetMovieRect( theLocationDesc, theMovie,
- &dispBounds);
- if(theErr) return theErr;
- } else {
- OffsetRect( &dispBounds,-dispBounds.left,-
- dispBounds.top);
- SetMovieBox(theMovie, &dispBounds);
- // Make sure window not under menu bar
- OffsetRect(&dispBounds,kDefaultOffset,kDefaultOffset);
- }
-
- /*
- Any time you are going to put up a dialog be sure to
- call AEInteractWithUser. The AppleEvent Manager will
- take care of posting notification and/or layer switching
- as needed.
- */
-
- theErr = AEInteractWithUser(kAEDefaultTimeout, NULL, NULL);
- if(theErr) return theErr;
-
- /* Set up our window */
- movieWindow = NewCWindow( 0L, &dispBounds,
- (StringPtr)myFSSpec.name,
- true,0,(WindowPtr)-1L,false,0L);
- if(movieWindow == NULL) {
- // Whoops, no window so BAIL;
- SysBeep(1);
- return memFullErr;
- }
-
- ShowWindow(movieWindow); // Make the window visible
- SetPort(movieWindow); // Set the part
- SetMovieGWorld(theMovie,NULL,NULL); // Set up the movie world
-
- /* Now we're ready to play the movie */
- GoToBeginningOfMovie(theMovie);// Rewind movie
- PrerollMovie(theMovie,0,0); // Get the movie ready to play
- SetMovieActive(theMovie,true); // Activate movie
- StartMovie(theMovie); // Start playing
-
- /*
- Play the movie to the end unless the mouse button has
- been pressed.
- */
- while ( !IsMovieDone(theMovie) && !Button())
- MoviesTask(theMovie,0);
-
- FlushEvents(everyEvent, 0); // Clean up spurious events
- Delay(duration, &finalTick); // Hold on the final fram
- /* Clean up and go home */
- DisposeMovie(theMovie); // Get rid of the movie
- CloseMovieFile(resRefNum); // Close movie file
- DisposeWindow(movieWindow); // And dispose our window
- return theErr;
- }
-
- ////////////////////////////////////////////////////////////////////
- // SetMovieRect
- // Set the position and bounding rectangle of our movie window.
- //
- //////////////////////////////////////////////////////////////////
-
- OSErr SetMovieRect( AEDesc theLocationDesc, Movie theMovie,
- Rect *ourRect) {
- long numberOfListItems = 0;
- OSErr theErr = noErr;
- AEKeyword theAEKeyword;
- DescType typeCode;
- Size actualSize;
- long pointLeft, pointTop,
- pointRight = kBogusNumber, // We use pointRight to see if
- pointBottom; // if we've gotten a point
- // or a rectangle
-
- /* Did we get passed a list? */
- if(theLocationDesc.descriptorType == typeAEList) {
-
- /* Get the data handle size to determine if point or rect */
- theErr = AECountItems( &theLocationDesc,
- &numberOfListItems);
- if(theErr) return theErr; // Bail on error!
- /* Must be two or four items in list */
- if(numberOfListItems != 2 && numberOfListItems != 4)
- return paramErr;
-
- /* If it's a point, just move the window. */
- if(numberOfListItems == 2) {
- theErr = AEGetNthPtr( &theLocationDesc, kLeft,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointLeft,
- sizeof(pointLeft),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- theErr = AEGetNthPtr( &theLocationDesc, kTop,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointTop,
- sizeof(pointTop),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- } else if(numberOfListItems == 4) { // It's a rectangle
- theErr = AEGetNthPtr( &theLocationDesc, kLeft,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointLeft,
- sizeof(pointLeft),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- theErr = AEGetNthPtr( &theLocationDesc, kTop,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointTop,
- sizeof(pointTop),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- theErr = AEGetNthPtr( &theLocationDesc, kRight,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointRight,
- sizeof(pointRight),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- theErr = AEGetNthPtr( &theLocationDesc, kBottom,
- typeLongInteger,
- &theAEKeyword, &typeCode,
- (Ptr)&pointBottom,
- sizeof(pointBottom),
- &actualSize);
- if(theErr) return theErr; // Just in case…
-
- }
- /* Is it a record? */
- } else if(theLocationDesc.descriptorType == typeAERecord) {
- /* Get the points out by key names */
-
- theErr = AEGetKeyPtr( &theLocationDesc, keyLeft,
- typeLongInteger,
- &typeCode, (Ptr)&pointLeft,
- sizeof(pointLeft),
- &actualSize);
- if(theErr) return theErr; // Must have these two
-
- theErr = AEGetKeyPtr( &theLocationDesc, keyTop,
- typeLongInteger,
- &typeCode, (Ptr)&pointTop,
- sizeof(pointTop),
- &actualSize);
- if(theErr) return theErr; // Must have these two
-
- theErr = AEGetKeyPtr( &theLocationDesc, keyRight,
- typeLongInteger,
- &typeCode, (Ptr)&pointRight,
- sizeof(pointRight),
- &actualSize);
- // Ignore this error
-
- theErr = AEGetKeyPtr( &theLocationDesc, keyBottom,
- typeLongInteger,
- &typeCode, (Ptr)&pointBottom,
- sizeof(pointBottom),
- &actualSize);
- // Ignore this error too, but clear our variable
- theErr = noErr;
- }
-
- if(pointRight == kBogusNumber) // We got a new origin...
- SetRect( ourRect, pointLeft, pointTop,
- (ourRect->right - ourRect->left) + pointLeft,
- (ourRect->bottom - ourRect->top) + pointTop);
- else // We got a new rectangle...
- SetRect( ourRect, pointLeft, pointTop,
- pointRight, pointBottom);
-
- /* Set topleft to 0,0 */
- OffsetRect(ourRect,-ourRect->left,-ourRect->top);
-
- /* Set the movie box to the new rect. */
- SetMovieBox(theMovie, ourRect);
-
- OffsetRect(ourRect,pointLeft, pointTop);
-
- return theErr;
- }